package uos.gcds;

import java.util.HashMap;
import java.util.Map;

import uos.util.StringUtil;

public class Catalog<T> {

	private Map<String, T> mapData;

	public Map<String, T> getTList() {
		return this.mapData;
	}
	
	public void setTList(HashMap<String, T> TList) {
		this.mapData = TList;
	}

	public T getItem(String T_Id) {
		return (T) this.getTList().get(T_Id);
	}
	
	public String addItem(T t) {
		HashMap<String, T> TMap = (HashMap<String, T>)this.getTList();
		String id = this.getNewId();
		if( TMap == null )
			TMap = new HashMap<String, T>();
		TMap.put(id, t);
		this.setTList(TMap);
		return id;
	}
	
	public void addItem(String id, T t) {
		HashMap<String, T> TMap = (HashMap<String, T>)this.getTList();
		if( TMap == null )
			TMap = new HashMap<String, T>();
		TMap.put(id, t);
		this.setTList(TMap);
	}
	
	public String getNewId(){
		String id = "i"+StringUtil.getTimeKey();
		return id;
	}
	
	public HashMap<String, T> getAllItems(){
		return (HashMap<String, T>)this.getTList();
	}
	
	public void delteItems(String[] T_id){
		
		if( T_id != null){
			
			HashMap<String, T> map = (HashMap<String, T>) this.getTList();
			for( int i=0; i<T_id.length; i++){
				map.remove(T_id[i]);
			}
			this.setTList(map);
		}
	}
	
	public void updateItem(String T_id, T t){
		HashMap<String, T> map = (HashMap<String, T>)this.getTList();
		
		if( map.containsKey(T_id) ){
			
			if( t instanceof Item){
				System.out.println(" Item ");
				Item tIns = (Item)map.get(T_id);
				tIns.updateItem((Item)t);
			}else{
				System.out.println(" Item NOT");
			}
			
		}
	}

}
